package com.linkoog.devtools.intellij.ui.table.cell_editor;


import org.jetbrains.annotations.NotNull;

import javax.swing.*;

public class JCheckBoxTableCellEditor extends AbstractDelegateTableCellEditor {

    public JCheckBoxTableCellEditor(){
        this(new JCheckBox());
    }

    public JCheckBoxTableCellEditor(@NotNull JCheckBox checkBox) {
        checkBox.setSelected(false);
        checkBox.setHorizontalAlignment(JCheckBox.CENTER);
        delegate = new EditorDelegate() {
            public void setValue(Object value) {
                boolean selected = false;
                if (value instanceof Boolean) {
                    selected = ((Boolean) value).booleanValue();
                } else if (value instanceof String) {
                    selected = value.equals("true");
                }
                checkBox.setSelected(selected);
            }

            public Object getCellEditorValue() {
                return Boolean.valueOf(checkBox.isSelected());
            }
        };
        checkBox.addActionListener(delegate);
        checkBox.setRequestFocusEnabled(false);
        editorComponent = checkBox;
    }

    @Override
    public JCheckBox getComponent() {
        return (JCheckBox) editorComponent;
    }

}
